<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修仙模拟器</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    <!-- 配置Tailwind自定义颜色和字体 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#8B5CF6', // 紫色作为主色调，代表神秘和仙气
                        secondary: '#10B981', // 绿色作为辅助色，代表生机
                        accent: '#F59E0B', // 金色作为强调色，代表稀有和尊贵
                        dark: '#1E293B',
                        light: '#F8FAFC'
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                        serif: ['"Noto Serif SC"', 'serif']
                    }
                }
            }
        }
    </script>
    
    <style type="text/tailwindcss">
        @layer utilities {
            .text-shadow {
                text-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .text-glow {
                text-shadow: 0 0 10px rgba(139, 92, 246, 0.6);
            }
            .bg-gradient-cultivation {
                background: linear-gradient(135deg, #0F172A 0%, #1E293B 100%);
            }
            .animate-float {
                animation: float 6s ease-in-out infinite;
            }
            @keyframes float {
                0% { transform: translateY(0px); }
                50% { transform: translateY(-10px); }
                100% { transform: translateY(0px); }
            }
            .animate-pulse-slow {
                animation: pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite;
            }
            
            /* 自定义滚动条样式 */
            .custom-scrollbar::-webkit-scrollbar {
                width: 6px;
            }
            .custom-scrollbar::-webkit-scrollbar-track {
                background: rgba(30, 41, 59, 0.5);
                border-radius: 10px;
            }
            .custom-scrollbar::-webkit-scrollbar-thumb {
                background: rgba(139, 92, 246, 0.5);
                border-radius: 10px;
            }
            .custom-scrollbar::-webkit-scrollbar-thumb:hover {
                background: rgba(139, 92, 246, 0.8);
            }
        }
    </style>
</head>
<body class="bg-gradient-cultivation text-light min-h-screen font-sans">
    <!-- 游戏容器 -->
    <div class="container mx-auto px-4 py-8 max-w-6xl">
        <!-- 开始界面 -->
        <div id="start-screen" class="flex flex-col items-center justify-center min-h-[70vh]">
            <h1 class="text-[clamp(2.5rem,6vw,4rem)] font-bold text-center mb-6 text-primary text-glow">修仙模拟器</h1>
            <p class="text-xl text-center mb-10 max-w-2xl text-gray-300">
                从一介凡人开始，踏上修仙之路。修炼、突破、渡劫，追求大道永恒。
                你的每一个选择都将影响最终的命运。
            </p>
            <div class="flex flex-col sm:flex-row gap-4">
                <button id="start-btn" class="px-8 py-3 bg-primary hover:bg-primary/80 text-white rounded-lg text-lg font-medium transition-all duration-300 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-primary/50">
                    <i class="fa fa-play mr-2"></i>开始修仙
                </button>
                <button id="help-btn" class="px-8 py-3 bg-dark hover:bg-dark/80 text-white rounded-lg text-lg font-medium transition-all duration-300 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-gray-500/50">
                    <i class="fa fa-question-circle mr-2"></i>游戏说明
                </button>
            </div>
            
            <!-- 装饰元素 -->
            <div class="absolute top-20 left-10 opacity-20 animate-float hidden md:block">
                <i class="fa fa-moon-o text-5xl text-primary"></i>
            </div>
            <div class="absolute bottom-20 right-10 opacity-20 animate-float" style="animation-delay: 2s;">
                <i class="fa fa-star text-5xl text-accent"></i>
            </div>
        </div>
        
        <!-- 帮助界面 -->
        <div id="help-screen" class="hidden">
            <button id="back-from-help" class="mb-6 px-4 py-2 bg-dark hover:bg-dark/80 text-white rounded-lg transition-all">
                <i class="fa fa-arrow-left mr-2"></i>返回
            </button>
            
            <h2 class="text-2xl font-bold mb-4 text-primary">游戏说明</h2>
            <div class="bg-dark/50 p-6 rounded-lg mb-6">
                <h3 class="text-xl font-semibold mb-3 text-accent">基本概念</h3>
                <ul class="list-disc pl-5 space-y-2 text-gray-300">
                    <li>修仙者有四个基本属性：根骨、悟性、气运和灵力</li>
                    <li>境界从低到高：炼气、筑基、金丹、元婴、化神、炼虚、合体、大乘、渡劫</li>
                    <li>每个境界有不同的寿元上限，突破境界可延长寿命</li>
                </ul>
            </div>
            
            <div class="bg-dark/50 p-6 rounded-lg mb-6">
                <h3 class="text-xl font-semibold mb-3 text-accent">行动选择</h3>
                <ul class="list-disc pl-5 space-y-2 text-gray-300">
                    <li>修炼：增加修为，提升灵力</li>
                    <li>冥想：提升悟性，更容易领悟功法</li>
                    <li>历练：可能遇到机缘或危险，提升根骨</li>
                    <li>突破：尝试晋升到下一境界</li>
                </ul>
            </div>
            
            <div class="bg-dark/50 p-6 rounded-lg">
                <h3 class="text-xl font-semibold mb-3 text-accent">游戏目标</h3>
                <p class="text-gray-300">
                    不断提升境界，最终成功渡劫飞升，成为仙尊。注意寿元限制，
                    若寿元耗尽还未突破，将功亏一篑。
                </p>
            </div>
        </div>
        
        <!-- 游戏主界面 -->
        <div id="game-screen" class="hidden">
            <!-- 顶部信息 -->
            <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6">
                <div>
                    <h2 class="text-2xl font-bold text-primary">修仙之路</h2>
                    <p id="age-display" class="text-gray-400">年龄: <span>16</span> 岁 | 寿元: <span>100</span> 年</p>
                </div>
                <div class="mt-4 md:mt-0">
                    <button id="restart-btn" class="px-4 py-2 bg-dark hover:bg-dark/80 text-white rounded-lg transition-all">
                        <i class="fa fa-refresh mr-1"></i>重新开始
                    </button>
                </div>
            </div>
            
            <!-- 主要内容区 -->
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                <!-- 左侧：角色状态 -->
                <div class="lg:col-span-1">
                    <div class="bg-dark/50 rounded-xl p-6 mb-6 border border-primary/20">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">角色状态</h3>
                        
                        <div class="flex justify-center mb-6">
                            <div class="w-32 h-32 rounded-full bg-primary/20 flex items-center justify-center animate-pulse-slow">
                                <i id="cultivation-icon" class="fa fa-user-circle text-5xl text-primary"></i>
                            </div>
                        </div>
                        
                        <div class="mb-4">
                            <p class="text-gray-400 mb-1">姓名</p>
                            <p id="name-display" class="text-xl font-medium">无尘子</p>
                        </div>
                        
                        <div class="mb-4">
                            <p class="text-gray-400 mb-1">境界</p>
                            <p id="realm-display" class="text-xl font-medium text-primary">炼气初期</p>
                        </div>
                        
                        <div class="mb-2">
                            <p class="text-gray-400 mb-1">修为进度</p>
                            <div class="w-full bg-dark rounded-full h-2.5">
                                <div id="cultivation-progress" class="bg-primary h-2.5 rounded-full" style="width: 20%"></div>
                            </div>
                            <p id="progress-text" class="text-right text-sm text-gray-500 mt-1">20/100</p>
                        </div>
                    </div>
                    
                    <!-- 属性面板 -->
                    <div class="bg-dark/50 rounded-xl p-6 border border-primary/20">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">基本属性</h3>
                        
                        <div class="space-y-4">
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">根骨</span>
                                    <span id="constitution-value" class="text-secondary">6</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="constitution-bar" style="width: 60%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响修炼效率和体魄强度</p>
                            </div>
                            
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">悟性</span>
                                    <span id="insight-value" class="text-secondary">4</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="insight-bar" style="width: 40%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响修炼速度和领悟能力</p>
                            </div>
                            
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">气运</span>
                                    <span id="luck-value" class="text-secondary">5</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="luck-bar" style="width: 50%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响随机事件的好坏</p>
                            </div>
                            
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">灵力</span>
                                    <span id="spirit-value" class="text-secondary">3</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="spirit-bar" style="width: 30%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响战斗能力和法术威力</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 右侧：行动和日志 -->
                <div class="lg:col-span-2 space-y-6">
                    <!-- 行动选择 -->
                    <div class="bg-dark/50 rounded-xl p-6 border border-primary/20">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">行动选择</h3>
                        
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                            <button id="meditate-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-moon-o text-2xl mb-2"></i>
                                <span>静心冥想</span>
                                <span class="text-xs text-gray-400 mt-1">提升悟性，领悟功法</span>
                            </button>
                            
                            <button id="cultivate-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-fire text-2xl mb-2"></i>
                                <span>潜心修炼</span>
                                <span class="text-xs text-gray-400 mt-1">增加修为，提升灵力</span>
                            </button>
                            
                            <button id="adventure-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-compass text-2xl mb-2"></i>
                                <span>外出历练</span>
                                <span class="text-xs text-gray-400 mt-1">遭遇机缘或危险，提升根骨</span>
                            </button>
                            
                            <button id="breakthrough-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-rocket text-2xl mb-2"></i>
                                <span>尝试突破</span>
                                <span class="text-xs text-gray-400 mt-1">冲击下一境界，有失败风险</span>
                            </button>
                        </div>
                    </div>
                    
                    <!-- 事件日志 -->
                    <div class="bg-dark/50 rounded-xl p-6 border border-primary/20">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">修仙日志</h3>
                        
                        <div id="event-log" class="h-64 overflow-y-auto pr-2 text-gray-300 space-y-3 custom-scrollbar">
                            <div class="p-2 bg-dark/70 rounded border-l-2 border-primary">
                                <p>你踏上了修仙之路，当前为炼气初期。</p>
                            </div>
                            <div class="p-2 bg-dark/70 rounded border-l-2 border-gray-500">
                                <p>师父告诉你："大道无情，修行在己。"</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 突破境界弹窗 -->
        <div id="breakthrough-modal" class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 hidden">
            <div class="bg-dark rounded-xl p-8 max-w-md w-full mx-4 border border-primary/30 transform transition-all">
                <h3 class="text-2xl font-bold mb-4 text-center text-accent">境界突破</h3>
                
                <div id="breakthrough-message" class="text-center mb-6 text-gray-300">
                    你准备突破到筑基期，成功率60%。突破过程中可能遭遇心魔侵袭！
                </div>
                
                <div class="flex justify-center gap-4">
                    <button id="confirm-breakthrough" class="px-6 py-2 bg-primary hover:bg-primary/80 text-white rounded-lg transition-all">
                        开始突破
                    </button>
                    <button id="cancel-breakthrough" class="px-6 py-2 bg-dark hover:bg-dark/80 text-white rounded-lg transition-all">
                        取消
                    </button>
                </div>
            </div>
        </div>
        
        <!-- 事件弹窗 -->
        <div id="event-modal" class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 hidden">
            <div class="bg-dark rounded-xl p-8 max-w-md w-full mx-4 border border-primary/30">
                <h3 id="event-title" class="text-2xl font-bold mb-4 text-center text-accent">奇遇</h3>
                
                <div id="event-description" class="text-center mb-6 text-gray-300">
                    你在山中发现了一株千年灵芝，服用后根骨提升了2点！
                </div>
                
                <div id="event-choices" class="flex flex-col gap-3 mb-6 hidden">
                    <!-- 动态生成选择按钮 -->
                </div>
                
                <button id="close-event" class="px-6 py-2 bg-primary hover:bg-primary/80 text-white rounded-lg transition-all w-full">
                    继续修仙
                </button>
            </div>
        </div>
        
        <!-- 游戏结束弹窗 -->
        <div id="game-over-modal" class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 hidden">
            <div class="bg-dark rounded-xl p-8 max-w-md w-full mx-4 border border-primary/30">
                <h3 id="game-over-title" class="text-2xl font-bold mb-4 text-center text-accent">寿终正寝</h3>
                
                <div id="game-over-message" class="text-center mb-6 text-gray-300">
                    你享年85岁，止步于金丹期，未能得道成仙。
                </div>
                
                <div class="mb-6 text-center">
                    <p class="text-gray-400">你的修仙成就</p>
                    <p id="final-achievement" class="text-xl font-medium text-primary">金丹真人</p>
                </div>
                
                <button id="restart-game" class="px-6 py-2 bg-primary hover:bg-primary/80 text-white rounded-lg transition-all w-full">
                    重新开始
                </button>
            </div>
        </div>
    </div>

    <script>
        // 游戏数据
        const gameData = {
            name: "无尘子",
            age: 16,
            lifespan: 100,
            realm: 0, // 0:炼气, 1:筑基, 2:金丹, 3:元婴, 4:化神, 5:炼虚, 6:合体, 7:大乘, 8:渡劫
            cultivation: 20, // 当前修为
            maxCultivation: 100, // 当前境界所需修为
            attributes: {
                constitution: 6, // 根骨
                insight: 4, // 悟性
                luck: 5, // 气运
                spirit: 3 // 灵力
            },
            log: [
                { text: "你踏上了修仙之路，当前为炼气初期。", type: "major" },
                { text: "师父告诉你：\"大道无情，修行在己。\"", type: "normal" }
            ]
        };
        
        // 境界数据
        const realms = [
            { name: "炼气初期", maxAge: 100, next: "炼气中期", required: 100 },
            { name: "炼气中期", maxAge: 120, next: "炼气后期", required: 200 },
            { name: "炼气后期", maxAge: 150, next: "筑基初期", required: 300 },
            { name: "筑基初期", maxAge: 200, next: "筑基中期", required: 500 },
            { name: "筑基中期", maxAge: 250, next: "筑基后期", required: 800 },
            { name: "筑基后期", maxAge: 300, next: "金丹初期", required: 1200 },
            { name: "金丹初期", maxAge: 500, next: "金丹中期", required: 2000 },
            { name: "金丹中期", maxAge: 600, next: "金丹后期", required: 3000 },
            { name: "金丹后期", maxAge: 800, next: "元婴初期", required: 5000 },
            { name: "元婴初期", maxAge: 1000, next: "元婴中期", required: 8000 },
            { name: "元婴中期", maxAge: 1200, next: "元婴后期", required: 12000 },
            { name: "元婴后期", maxAge: 1500, next: "化神初期", required: 20000 },
            { name: "化神初期", maxAge: 2000, next: "化神中期", required: 30000 },
            { name: "化神中期", maxAge: 2500, next: "化神后期", required: 50000 },
            { name: "化神后期", maxAge: 3000, next: "炼虚初期", required: 80000 },
            { name: "炼虚初期", maxAge: 5000, next: "炼虚中期", required: 120000 },
            { name: "炼虚中期", maxAge: 6000, next: "炼虚后期", required: 200000 },
            { name: "炼虚后期", maxAge: 8000, next: "合体初期", required: 300000 },
            { name: "合体初期", maxAge: 10000, next: "合体中期", required: 500000 },
            { name: "合体中期", maxAge: 15000, next: "合体后期", required: 800000 },
            { name: "合体后期", maxAge: 20000, next: "大乘初期", required: 1200000 },
            { name: "大乘初期", maxAge: 50000, next: "大乘中期", required: 2000000 },
            { name: "大乘中期", maxAge: 80000, next: "大乘后期", required: 3000000 },
            { name: "大乘后期", maxAge: 100000, next: "渡劫期", required: 5000000 },
            { name: "渡劫期", maxAge: 200000, next: "飞升", required: 10000000 }
        ];
        
        // 随机事件集合 - 规范化结构，包含名称、描述、效果、触发概率和类型
        const eventSystem = {
            events: [
                // 正面事件
                {
                    id: "meet_immortal",
                    name: "偶遇仙缘",
                    description: "你在山中采药时，偶遇一位隐世高人，他指点了你修炼中的一个误区。",
                    effect: function() {
                        const gain = Math.floor(Math.random() * 2) + 1;
                        gameData.attributes.insight += gain;
                        addLog(`偶遇隐世高人指点，悟性提升了${gain}点。`, "good");
                    },
                    probability: 10, // 基础概率
                    type: "good"
                },
                {
                    id: "find_treasure",
                    name: "天材地宝",
                    description: "你发现了一株千年灵芝，服用后感觉浑身舒畅。",
                    effect: function() {
                        const gain = Math.floor(Math.random() * 2) + 1;
                        gameData.attributes.constitution += gain;
                        const cultGain = Math.floor(Math.random() * 50) + 30;
                        gameData.cultivation += cultGain;
                        addLog(`服用千年灵芝，根骨提升了${gain}点，修为增加${cultGain}点。`, "good");
                    },
                    probability: 12,
                    type: "good"
                },
                {
                    id: "enlightened",
                    name: "功法顿悟",
                    description: "在一次冥想中，你突然领悟了功法的精髓。",
                    effect: function() {
                        const gain = Math.floor(Math.random() * 2) + 1;
                        gameData.attributes.spirit += gain;
                        const cultGain = Math.floor(Math.random() * 80) + 50;
                        gameData.cultivation += cultGain;
                        addLog(`顿悟功法精髓，灵力提升了${gain}点，修为增加${cultGain}点。`, "good");
                    },
                    probability: 8,
                    type: "good"
                },
                {
                    id: "ancient_ruins",
                    name: "古迹探寻",
                    description: "你发现一处古老的修仙遗迹，从中获得了一本残缺的功法。",
                    effect: function() {
                        const gain = Math.floor(Math.random() * 3) + 1;
                        gameData.attributes.insight += gain;
                        addLog(`在古迹中获得残缺功法，悟性提升了${gain}点。`, "good");
                    },
                    probability: 7,
                    type: "good"
                },
                {
                    id: "spiritual_spring",
                    name: "灵泉洗礼",
                    description: "你发现一处蕴含浓郁灵气的泉眼，在其中沐浴后修为大进。",
                    effect: function() {
                        const cultGain = Math.floor(Math.random() * 100) + 80;
                        gameData.cultivation += cultGain;
                        addLog(`在灵泉中沐浴，修为大幅增加${cultGain}点。`, "good");
                    },
                    probability: 9,
                    type: "good"
                },
                
                // 负面事件
                {
                    id: "monster_attack",
                    name: "遭遇妖兽",
                    description: "历练途中遭遇凶猛妖兽袭击，你奋力抵抗...",
                    effect: function() {
                        // 受伤程度受灵力影响，灵力越高受伤越轻
                        const injury = Math.max(1, 3 - Math.floor(gameData.attributes.spirit / 5));
                        gameData.attributes.constitution -= injury;
                        const cultLoss = Math.floor(Math.random() * 30) + 20 * injury;
                        gameData.cultivation -= cultLoss;
                        if (gameData.cultivation < 0) gameData.cultivation = 0;
                        
                        let message = `遭遇妖兽袭击，`;
                        if (injury === 1) {
                            message += `受了点轻伤，根骨下降1点，修为减少${cultLoss}点。`;
                        } else if (injury === 2) {
                            message += `受了重伤，根骨下降2点，修为减少${cultLoss}点。`;
                        } else {
                            message += `身受重创，根骨下降3点，修为减少${cultLoss}点。`;
                        }
                        addLog(message, "bad");
                    },
                    probability: 10,
                    type: "bad"
                },
                {
                    id: "demon_influence",
                    name: "心魔入侵",
                    description: "修炼时心不静，引动心魔，险些走火入魔。",
                    effect: function() {
                        const loss = Math.floor(Math.random() * 2) + 1;
                        gameData.attributes.insight -= loss;
                        const cultLoss = Math.floor(Math.random() * 50) + 30;
                        gameData.cultivation -= cultLoss;
                        if (gameData.cultivation < 0) gameData.cultivation = 0;
                        addLog(`心魔入侵，悟性下降${loss}点，修为减少${cultLoss}点。`, "bad");
                    },
                    probability: 8,
                    type: "bad"
                },
                {
                    id: "robber_attack",
                    name: "遭遇劫匪",
                    description: "归途遭遇修仙败类抢劫，你虽保住性命但损失惨重。",
                    effect: function() {
                        const cultLoss = Math.floor(Math.random() * 100) + 50;
                        gameData.cultivation -= cultLoss;
                        if (gameData.cultivation < 0) gameData.cultivation = 0;
                        addLog(`遭遇劫匪，损失惨重，修为减少${cultLoss}点。`, "bad");
                    },
                    probability: 6,
                    type: "bad"
                },
                {
                    id: "natural_disaster",
                    name: "天灾降临",
                    description: "修炼之地突遭异变，灵气紊乱，影响了你的修炼。",
                    effect: function() {
                        gameData.age += 1; // 多消耗一年
                        addLog(`天灾降临，灵气紊乱，白白浪费了一年修炼时间。`, "bad");
                    },
                    probability: 5,
                    type: "bad"
                },
                
                // 选择事件
                {
                    id: "tough_choice",
                    name: "艰难抉择",
                    description: "你在山洞中发现了两本秘籍，只能选择其一：",
                    choices: [
                        {
                            text: "《锻体诀》",
                            effect: function() {
                                const gain = Math.floor(Math.random() * 3) + 2;
                                gameData.attributes.constitution += gain;
                                addLog(`选择修炼《锻体诀》，根骨大幅提升${gain}点。`, "good");
                            }
                        },
                        {
                            text: "《清心经》",
                            effect: function() {
                                const gain = Math.floor(Math.random() * 3) + 2;
                                gameData.attributes.insight += gain;
                                addLog(`选择修炼《清心经》，悟性大幅提升${gain}点。`, "good");
                            }
                        }
                    ],
                    probability: 9,
                    type: "choice"
                },
                {
                    id: "sect_call",
                    name: "门派征召",
                    description: "宗门发布紧急任务，需要弟子前往支援：",
                    choices: [
                        {
                            text: "接受任务",
                            effect: function() {
                                const luckGain = Math.floor(Math.random() * 2) + 1;
                                gameData.attributes.luck += luckGain;
                                const cultGain = Math.floor(Math.random() * 150) + 100;
                                gameData.cultivation += cultGain;
                                gameData.age += 2; // 任务耗时2年
                                addLog(`完成门派任务，气运提升${luckGain}点，修为增加${cultGain}点，但耗费了2年时间。`, "mixed");
                            }
                        },
                        {
                            text: "婉言拒绝",
                            effect: function() {
                                const luckLoss = Math.floor(Math.random() * 2) + 1;
                                gameData.attributes.luck -= luckLoss;
                                addLog(`拒绝门派任务，引起长老不满，气运下降${luckLoss}点。`, "bad");
                            }
                        }
                    ],
                    probability: 7,
                    type: "choice"
                },
                {
                    id: "help_stranger",
                    name: "路见不平",
                    description: "你遇到一位被追杀的修士，你决定：",
                    choices: [
                        {
                            text: "出手相助",
                            effect: function() {
                                // 有风险但可能有回报
                                if (Math.random() < 0.6 + gameData.attributes.luck / 100) {
                                    const gain = Math.floor(Math.random() * 3) + 2;
                                    gameData.attributes.luck += gain;
                                    const cultGain = Math.floor(Math.random() * 100) + 50;
                                    gameData.cultivation += cultGain;
                                    addLog(`成功救助修士，对方赠予厚礼，气运提升${gain}点，修为增加${cultGain}点。`, "good");
                                } else {
                                    const loss = Math.floor(Math.random() * 2) + 1;
                                    gameData.attributes.spirit -= loss;
                                    const cultLoss = Math.floor(Math.random() * 80) + 30;
                                    gameData.cultivation -= cultLoss;
                                    if (gameData.cultivation < 0) gameData.cultivation = 0;
                                    addLog(`救助过程中受了重伤，灵力下降${loss}点，修为减少${cultLoss}点。`, "bad");
                                }
                            }
                        },
                        {
                            text: "袖手旁观",
                            effect: function() {
                                gameData.attributes.luck -= 1;
                                addLog(`你选择了袖手旁观，心中略有不安，气运下降1点。`, "bad");
                            }
                        }
                    ],
                    probability: 8,
                    type: "choice"
                }
            ],
            
            // 获取随机事件，受气运影响
            getRandomEvent: function() {
                // 根据气运调整事件概率
                const luckFactor = gameData.attributes.luck / 5; // 气运影响因子
                
                // 创建加权事件列表
                const weightedEvents = [];
                this.events.forEach(event => {
                    let adjustedProbability = event.probability;
                    
                    // 根据事件类型和运气调整概率
                    if (event.type === "good") {
                        adjustedProbability *= luckFactor; // 运气好，好事概率增加
                    } else if (event.type === "bad") {
                        adjustedProbability *= (2 - luckFactor); // 运气好，坏事概率减少
                    }
                    
                    // 确保概率为正数
                    adjustedProbability = Math.max(1, Math.round(adjustedProbability));
                    
                    // 添加到加权列表
                    for (let i = 0; i < adjustedProbability; i++) {
                        weightedEvents.push(event);
                    }
                });
                
                // 随机选择一个事件
                return weightedEvents[Math.floor(Math.random() * weightedEvents.length)];
            }
        };
        
        // DOM元素
        const startScreen = document.getElementById('start-screen');
        const helpScreen = document.getElementById('help-screen');
        const gameScreen = document.getElementById('game-screen');
        const startBtn = document.getElementById('start-btn');
        const helpBtn = document.getElementById('help-btn');
        const backFromHelpBtn = document.getElementById('back-from-help');
        const restartBtn = document.getElementById('restart-btn');
        const restartGameBtn = document.getElementById('restart-game');
        
        // 行动按钮
        const meditateBtn = document.getElementById('meditate-btn');
        const cultivateBtn = document.getElementById('cultivate-btn');
        const adventureBtn = document.getElementById('adventure-btn');
        const breakthroughBtn = document.getElementById('breakthrough-btn');
        
        // 弹窗相关
        const breakthroughModal = document.getElementById('breakthrough-modal');
        const confirmBreakthroughBtn = document.getElementById('confirm-breakthrough');
        const cancelBreakthroughBtn = document.getElementById('cancel-breakthrough');
        const eventModal = document.getElementById('event-modal');
        const eventTitle = document.getElementById('event-title');
        const eventDescription = document.getElementById('event-description');
        const eventChoices = document.getElementById('event-choices');
        const closeEventBtn = document.getElementById('close-event');
        const gameOverModal = document.getElementById('game-over-modal');
        const gameOverTitle = document.getElementById('game-over-title');
        const gameOverMessage = document.getElementById('game-over-message');
        const finalAchievement = document.getElementById('final-achievement');
        
        // 初始化事件监听
        function initEventListeners() {
            startBtn.addEventListener('click', startGame);
            helpBtn.addEventListener('click', showHelp);
            backFromHelpBtn.addEventListener('click', hideHelp);
            restartBtn.addEventListener('click', restartGame);
            restartGameBtn.addEventListener('click', restartGame);
            
            // 行动按钮
            meditateBtn.addEventListener('click', meditate);
            cultivateBtn.addEventListener('click', cultivate);
            adventureBtn.addEventListener('click', adventure);
            breakthroughBtn.addEventListener('click', showBreakthroughModal);
            
            // 弹窗按钮
            confirmBreakthroughBtn.addEventListener('click', attemptBreakthrough);
            cancelBreakthroughBtn.addEventListener('click', hideBreakthroughModal);
            closeEventBtn.addEventListener('click', hideEventModal);
        }
        
        // 开始游戏
        function startGame() {
            startScreen.classList.add('hidden');
            gameScreen.classList.remove('hidden');
            updateUI();
        }
        
        // 显示帮助
        function showHelp() {
            startScreen.classList.add('hidden');
            helpScreen.classList.remove('hidden');
        }
        
        // 隐藏帮助
        function hideHelp() {
            helpScreen.classList.add('hidden');
            startScreen.classList.remove('hidden');
        }
        
        // 重新开始游戏
        function restartGame() {
            // 重置游戏数据
            gameData.age = 16;
            gameData.realm = 0;
            gameData.cultivation = 20;
            gameData.maxCultivation = realms[0].required;
            gameData.lifespan = realms[0].maxAge;
            gameData.attributes = {
                constitution: Math.floor(Math.random() * 5) + 3,
                insight: Math.floor(Math.random() * 5) + 3,
                luck: Math.floor(Math.random() * 5) + 3,
                spirit: Math.floor(Math.random() * 5) + 3
            };
            gameData.log = [
                { text: "你踏上了修仙之路，当前为炼气初期。", type: "major" },
                { text: "师父告诉你：\"大道无情，修行在己。\"", type: "normal" }
            ];
            
            // 隐藏所有弹窗
            breakthroughModal.classList.add('hidden');
            eventModal.classList.add('hidden');
            gameOverModal.classList.add('hidden');
            
            // 显示游戏界面
            startScreen.classList.add('hidden');
            helpScreen.classList.add('hidden');
            gameScreen.classList.remove('hidden');
            
            updateUI();
        }
        
        // 更新UI显示
        function updateUI() {
            // 更新年龄和寿元
            document.querySelector('#age-display span:first-child').textContent = gameData.age;
            document.querySelector('#age-display span:last-child').textContent = gameData.lifespan;
            
            // 更新姓名和境界
            document.getElementById('name-display').textContent = gameData.name;
            document.getElementById('realm-display').textContent = realms[gameData.realm].name;
            
            // 更新修为进度
            const progressPercent = Math.min(100, (gameData.cultivation / gameData.maxCultivation) * 100);
            document.getElementById('cultivation-progress').style.width = `${progressPercent}%`;
            document.getElementById('progress-text').textContent = `${gameData.cultivation}/${gameData.maxCultivation}`;
            
            // 更新属性
            for (const [key, value] of Object.entries(gameData.attributes)) {
                document.getElementById(`${key}-value`).textContent = value;
                document.getElementById(`${key}-bar`).style.width = `${Math.min(100, value * 10)}%`;
            }
            
            // 更新日志
            updateLog();
            
            // 根据境界更新图标
            updateCultivationIcon();
        }
        
        // 更新修仙图标
        function updateCultivationIcon() {
            const iconElement = document.getElementById('cultivation-icon');
            iconElement.className = ''; // 清除现有类
            
            if (gameData.realm < 3) { // 炼气、筑基
                iconElement.className = 'fa fa-user-circle text-5xl text-primary';
            } else if (gameData.realm < 6) { // 金丹
                iconElement.className = 'fa fa-diamond text-5xl text-accent';
            } else if (gameData.realm < 9) { // 元婴
                iconElement.className = 'fa fa-child text-5xl text-secondary';
            } else if (gameData.realm < 15) { // 化神、炼虚
                iconElement.className = 'fa fa-ghost text-5xl text-purple-400';
            } else if (gameData.realm < 21) { // 合体、大乘
                iconElement.className = 'fa fa-star text-5xl text-yellow-400';
            } else { // 渡劫
                iconElement.className = 'fa fa-bolt text-5xl text-yellow-500';
            }
        }
        
        // 更新日志显示
        function updateLog() {
            const logElement = document.getElementById('event-log');
            logElement.innerHTML = '';
            
            // 只显示最近10条日志
            const recentLogs = gameData.log.slice(-10);
            
            recentLogs.forEach(entry => {
                const logItem = document.createElement('div');
                logItem.className = 'p-2 bg-dark/70 rounded border-l-2';
                
                // 根据日志类型设置边框颜色
                switch(entry.type) {
                    case 'major':
                        logItem.classList.add('border-primary');
                        break;
                    case 'good':
                        logItem.classList.add('border-secondary');
                        break;
                    case 'bad':
                        logItem.classList.add('border-red-500');
                        break;
                    case 'mixed':
                        logItem.classList.add('border-accent');
                        break;
                    default:
                        logItem.classList.add('border-gray-500');
                }
                
                logItem.innerHTML = `<p>${entry.text}</p>`;
                logElement.appendChild(logItem);
            });
            
            // 滚动到底部
            logElement.scrollTop = logElement.scrollHeight;
        }
        
        // 添加日志条目
        function addLog(text, type = 'normal') {
            gameData.log.push({ text, type });
        }
        
        // 静心冥想
        function meditate() {
            gameData.age += 1;
            
            // 提升悟性，受当前悟性影响
            const baseGain = Math.floor(Math.random() * 2) + 1;
            const insightGain = baseGain + Math.floor(gameData.attributes.insight / 10);
            gameData.attributes.insight += insightGain;
            
            // 少量提升修为，受悟性影响
            const cultGain = Math.floor(Math.random() * 10) + 5 + Math.floor(gameData.attributes.insight / 3);
            // 检查是否达到修为上限
            if (gameData.cultivation + cultGain > gameData.maxCultivation) {
                const actualGain = gameData.maxCultivation - gameData.cultivation;
                gameData.cultivation = gameData.maxCultivation;
                addLog(`经过一年冥想，你的悟性提升了${insightGain}点，修为增加了${actualGain}点，已达到当前境界上限，需要突破！`, "mixed");
            } else {
                gameData.cultivation += cultGain;
                addLog(`经过一年冥想，你的悟性提升了${insightGain}点，修为增加了${cultGain}点。`, "good");
            }
            
            // 有几率领悟，受悟性影响
            if (Math.random() < 0.3 + gameData.attributes.insight / 100) {
                const spiritGain = Math.floor(Math.random() * 2) + 1;
                gameData.attributes.spirit += spiritGain;
                addLog(`冥想中有所领悟，灵力提升了${spiritGain}点！`, "major");
            }
            
            processTurn();
        }
        
        // 潜心修炼
        function cultivate() {
            gameData.age += 1;
            
            // 提升修为，受灵力和根骨双重影响，优化算法
            const baseGain = Math.floor(Math.random() * 30) + 20;
            // 根骨和悟性共同影响修炼效率
            const efficiency = 1 + (gameData.attributes.constitution + gameData.attributes.insight) / 20;
            const cultGain = Math.floor(baseGain * efficiency);
            
            // 检查是否达到修为上限
            if (gameData.cultivation + cultGain > gameData.maxCultivation) {
                const actualGain = gameData.maxCultivation - gameData.cultivation;
                gameData.cultivation = gameData.maxCultivation;
                addLog(`刻苦修炼一年，修为增加了${actualGain}点，已达到当前境界上限，需要突破！`, "mixed");
            } else {
                gameData.cultivation += cultGain;
                // 小几率提升灵力
                if (Math.random() < 0.2) {
                    gameData.attributes.spirit += 1;
                    addLog(`刻苦修炼一年，修为增加了${cultGain}点，灵力也提升了1点！`, "good");
                } else {
                    addLog(`刻苦修炼一年，修为增加了${cultGain}点。`, "normal");
                }
            }
            
            processTurn();
        }
        
        // 外出历练
        function adventure() {
            gameData.age += 1;
            
            // 历练有几率遇到事件，受气运影响
            const eventChance = 0.6 + (gameData.attributes.luck - 5) / 100 * 20;
            if (Math.random() < Math.min(0.9, eventChance)) {
                triggerRandomEvent();
            } else {
                // 普通历练，提升根骨
                const constitutionGain = Math.floor(Math.random() * 2) + 1;
                gameData.attributes.constitution += constitutionGain;
                
                // 少量提升修为
                const cultGain = Math.floor(Math.random() * 15) + 5;
                // 检查是否达到修为上限
                if (gameData.cultivation + cultGain > gameData.maxCultivation) {
                    const actualGain = gameData.maxCultivation - gameData.cultivation;
                    gameData.cultivation = gameData.maxCultivation;
                    addLog(`历练一年，根骨提升了${constitutionGain}点，修为增加了${actualGain}点，已达到当前境界上限，需要突破！`, "mixed");
                } else {
                    gameData.cultivation += cultGain;
                    addLog(`历练一年，根骨提升了${constitutionGain}点，修为增加了${cultGain}点。`, "normal");
                }
                processTurn();
            }
        }
        
        // 触发随机事件
        function triggerRandomEvent() {
            const randomEvent = eventSystem.getRandomEvent();
            
            eventTitle.textContent = randomEvent.name;
            eventDescription.textContent = randomEvent.description;
            
            // 处理选择型事件
            if (randomEvent.choices) {
                eventChoices.innerHTML = '';
                eventChoices.classList.remove('hidden');
                
                randomEvent.choices.forEach(choice => {
                    const btn = document.createElement('button');
                    btn.className = 'px-4 py-2 bg-primary/20 hover:bg-primary/30 text-white rounded-lg transition-all';
                    btn.textContent = choice.text;
                    btn.addEventListener('click', () => {
                        choice.effect();
                        eventChoices.classList.add('hidden');
                        hideEventModal();
                        processTurn();
                    });
                    eventChoices.appendChild(btn);
                });
            } else {
                eventChoices.classList.add('hidden');
                // 非选择型事件在关闭弹窗时执行效果
                closeEventBtn.onclick = function() {
                    randomEvent.effect();
                    hideEventModal();
                    processTurn();
                };
            }
            
            eventModal.classList.remove('hidden');
        }
        
        // 显示突破弹窗
        function showBreakthroughModal() {
            // 检查是否可以突破
            if (gameData.cultivation < gameData.maxCultivation) {
                addLog("修为不足，无法突破境界。", "bad");
                updateUI();
                return;
            }
            
            const successRate = calculateBreakthroughRate();
            document.getElementById('breakthrough-message').textContent = 
                `你准备突破到${realms[gameData.realm + 1].name}，成功率${successRate}%。突破过程中可能遭遇心魔侵袭！`;
            
            breakthroughModal.classList.remove('hidden');
        }
        
        // 隐藏突破弹窗
        function hideBreakthroughModal() {
            breakthroughModal.classList.add('hidden');
        }
        
        // 隐藏事件弹窗
        function hideEventModal() {
            eventModal.classList.add('hidden');
            // 重置关闭按钮事件
            closeEventBtn.onclick = function() {
                hideEventModal();
            };
        }
        
        // 计算突破成功率
        function calculateBreakthroughRate() {
            // 基础成功率50%，受悟性和根骨影响更大
            let rate = 50 + (gameData.attributes.insight - 5) * 4 + (gameData.attributes.constitution - 5) * 3;
            // 气运加成
            rate += (gameData.attributes.luck - 5) * 2;
            // 最低30%，最高95%
            return Math.max(30, Math.min(95, Math.round(rate)));
        }
        
        // 尝试突破境界
        function attemptBreakthrough() {
            hideBreakthroughModal();
            
            gameData.age += 1; // 突破消耗一年时间
            
            const successRate = calculateBreakthroughRate();
            const success = Math.random() * 100 < successRate;
            
            if (success) {
                // 突破成功
                gameData.realm += 1;
                
                // 特殊处理：飞升成功
                if (gameData.realm >= realms.length) {
                    gameData.realm = realms.length - 1;
                    showGameOver(true);
                    return;
                }
                
                // 更新最大修为和寿元
                gameData.maxCultivation = realms[gameData.realm].required;
                gameData.lifespan = realms[gameData.realm].maxAge;
                
                // 突破后修为重置一部分
                gameData.cultivation = Math.floor(gameData.maxCultivation * 0.2);
                
                // 属性提升，根据当前境界提升幅度不同
                const baseGain = Math.floor(gameData.realm / 3) + 1;
                gameData.attributes.constitution += Math.floor(Math.random() * 3) + baseGain;
                gameData.attributes.insight += Math.floor(Math.random() * 3) + baseGain;
                gameData.attributes.spirit += Math.floor(Math.random() * 3) + baseGain + 1;
                
                addLog(`恭喜！你成功突破到${realms[gameData.realm].name}！各项属性得到提升。`, "major");
            } else {
                // 突破失败
                gameData.cultivation = Math.floor(gameData.cultivation * 0.7); // 修为下降
                gameData.attributes.spirit -= 1; // 灵力受损
                
                // 有几率受伤更重，受根骨影响
                if (Math.random() < 0.3 - (gameData.attributes.constitution - 5) / 100 * 10) {
                    gameData.attributes.constitution -= 1;
                    addLog(`突破失败！灵力受损，根骨也受到影响，修为大幅下降。`, "bad");
                } else {
                    addLog(`突破失败！灵力受损，修为下降。`, "bad");
                }
            }
            
            processTurn();
        }
        
        // 处理回合结束
        function processTurn() {
            // 检查是否寿终正寝
            if (gameData.age >= gameData.lifespan) {
                showGameOver(false);
                return;
            }
            
            // 随机气运事件，受气运影响更大
            if (Math.random() < 0.1 + gameData.attributes.luck / 100) {
                const luckEvent = Math.random() < 0.6 + (gameData.attributes.luck - 5) / 10 ? 
                    { text: "今日气运旺盛，修为略有精进。", gain: Math.floor(Math.random() * 20) + 10 } :
                    { text: "今日气运不佳，修炼效果甚微。", gain: Math.floor(Math.random() * 5) - 10 };
                
                // 检查是否达到修为上限
                if (gameData.cultivation + luckEvent.gain > gameData.maxCultivation) {
                    const actualGain = gameData.maxCultivation - gameData.cultivation;
                    gameData.cultivation = gameData.maxCultivation;
                    addLog(`今日气运旺盛，但修为已达上限，仅增加了${actualGain}点。`, "mixed");
                } else {
                    gameData.cultivation += luckEvent.gain;
                    if (gameData.cultivation < 0) gameData.cultivation = 0;
                    addLog(luckEvent.text, luckEvent.gain > 0 ? "good" : "bad");
                }
            }
            
            updateUI();
        }
        
        // 显示游戏结束
        function showGameOver(isSuccess) {
            if (isSuccess) {
                gameOverTitle.textContent = "飞升成功！";
                gameOverMessage.textContent = `你成功渡过天劫，破碎虚空，飞升成仙！享年${gameData.age}岁。`;
                finalAchievement.textContent = "九天仙尊";
            } else {
                gameOverTitle.textContent = "寿终正寝";
                gameOverMessage.textContent = `你享年${gameData.age}岁，止步于${realms[gameData.realm].name}，未能得道成仙。`;
                
                // 根据最终境界设置成就
                if (gameData.realm < 3) finalAchievement.textContent = "凡俗修士";
                else if (gameData.realm < 6) finalAchievement.textContent = "筑基高人";
                else if (gameData.realm < 9) finalAchievement.textContent = "金丹真人";
                else if (gameData.realm < 15) finalAchievement.textContent = "元婴大能";
                else if (gameData.realm < 21) finalAchievement.textContent = "化神巨擘";
                else finalAchievement.textContent = "渡劫准仙";
            }
            
            gameOverModal.classList.remove('hidden');
        }
        
        // 初始化游戏
        function init() {
            initEventListeners();
            // 随机生成初始属性
            gameData.attributes = {
                constitution: Math.floor(Math.random() * 5) + 3,
                insight: Math.floor(Math.random() * 5) + 3,
                luck: Math.floor(Math.random() * 5) + 3,
                spirit: Math.floor(Math.random() * 5) + 3
            };
        }
        
        // 启动游戏
        init();
    </script>
</body>
</html>
    